import { Memento } from 'vscode';

interface Tokens {
  [host: string]: {
    token: string;
  };
}

export function setToken(memento: Memento, provider: string, token: string): void {
  if (token) {
    const tokens = getToken(memento);
    tokens[provider] = {
      token,
    };
    memento.update('tokens', tokens);
  }
};

export function getToken(memento: Memento): Tokens {
  return memento.get<Tokens>('tokens', {});
}

export function removeToken(memento: Memento, provider: string): void {
  const tokens: Tokens | undefined = memento.get('tokens');
  if (tokens) {
    delete tokens[provider];
    memento.update('tokens', tokens);
  }
}
